/*
Copyright [2023] [fuwen_soft]
*/
package vip.xiaonuo.modular.xyrelationship.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import vip.xiaonuo.core.consts.CommonConstant;
import vip.xiaonuo.core.enums.CommonStatusEnum;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.core.factory.PageFactory;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.util.PoiUtil;
import vip.xiaonuo.modular.xyrelationship.entity.XyRelationship;
import vip.xiaonuo.modular.xyrelationship.enums.XyRelationshipExceptionEnum;
import vip.xiaonuo.modular.xyrelationship.mapper.XyRelationshipMapper;
import vip.xiaonuo.modular.xyrelationship.param.XyRelationshipParam;
import vip.xiaonuo.modular.xyrelationship.service.XyRelationshipService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.List;

/**
 * 族员关系service接口实现类
 *
 * @author yanwei
 * @date 2023-05-06 14:15:26
 */
@Service
public class XyRelationshipServiceImpl extends ServiceImpl<XyRelationshipMapper, XyRelationship> implements XyRelationshipService {

    @Override
    public PageResult<XyRelationship> page(XyRelationshipParam xyRelationshipParam) {
        QueryWrapper<XyRelationship> queryWrapper = new QueryWrapper<>();
        if (ObjectUtil.isNotNull(xyRelationshipParam)) {

            // 根据唯一编码 查询
            if (ObjectUtil.isNotEmpty(xyRelationshipParam.getCodeUni())) {
                queryWrapper.lambda().eq(XyRelationship::getCodeUni, xyRelationshipParam.getCodeUni());
            }
            // 根据名字 查询
            if (ObjectUtil.isNotEmpty(xyRelationshipParam.getRelationship())) {
                queryWrapper.lambda().eq(XyRelationship::getRelationship, xyRelationshipParam.getRelationship());
            }
        }
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<XyRelationship> list(XyRelationshipParam xyRelationshipParam) {
        return this.list();
    }

    @Override
    public void add(XyRelationshipParam xyRelationshipParam) {
        XyRelationship xyRelationship = new XyRelationship();
        BeanUtil.copyProperties(xyRelationshipParam, xyRelationship);
        this.save(xyRelationship);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(List<XyRelationshipParam> xyRelationshipParamList) {
        xyRelationshipParamList.forEach(xyRelationshipParam -> {
            this.removeById(xyRelationshipParam.getId());
        });
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void edit(XyRelationshipParam xyRelationshipParam) {
        XyRelationship xyRelationship = this.queryXyRelationship(xyRelationshipParam);
        BeanUtil.copyProperties(xyRelationshipParam, xyRelationship);
        this.updateById(xyRelationship);
    }

    @Override
    public XyRelationship detail(XyRelationshipParam xyRelationshipParam) {
        return this.queryXyRelationship(xyRelationshipParam);
    }

    /**
     * 获取族员关系
     *
     * @author yanwei
     * @date 2023-05-06 14:15:26
     */
    private XyRelationship queryXyRelationship(XyRelationshipParam xyRelationshipParam) {
        XyRelationship xyRelationship = this.getById(xyRelationshipParam.getId());
        if (ObjectUtil.isNull(xyRelationship)) {
            throw new ServiceException(XyRelationshipExceptionEnum.NOT_EXIST);
        }
        return xyRelationship;
    }

    @Override
    public void export(XyRelationshipParam xyRelationshipParam) {
        List<XyRelationship> list = this.list(xyRelationshipParam);
        PoiUtil.exportExcelWithStream("SnowyXyRelationship.xls", XyRelationship.class, list);
    }

}
